package com.zyasdk.util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;

public class SimpleHttp {

	public interface ResultCallBack {
		void result(String result);
	}

	public static void request(final String url, final ResultCallBack callBack) {
		new AsyncTask<Void, String, String>() {

			@Override
			protected String doInBackground(Void... params) {
				String result = null;
				HttpGet httpRequst = new HttpGet(url);
				HttpResponse httpResponse = null;
				try {
					httpResponse = new DefaultHttpClient().execute(httpRequst);// 其中HttpGet是HttpUriRequst的子类
					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						result = EntityUtils.toString(httpResponse.getEntity());// 取出应答字符串
						return result;
					}

				} catch (IOException e) {
					e.printStackTrace();
					result = e.getMessage().toString();
				} finally {
					httpRequst.abort();
				}

				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				if (callBack != null)
					callBack.result(result);
			}
		}.execute();

	}
}
